﻿#include "tm_sshconnectwgt.h"

#include <QDir>
#include <QHeaderView>
#include <QTimer>


TM_SSHConnectWGT::TM_SSHConnectWGT()
{
    setStyleSheet("background-color:rgb(80,80,80);color:rgba(255,255,255);");
    //setFixedHeight(400);
    resize(700,400);
    initUI();
    m_uploader = new SecureFileUploader();
    connect(this,&TM_SSHConnectWGT::sigPush,m_uploader,&SecureFileUploader::upload);
    connect(this,&TM_SSHConnectWGT::sigPull,m_uploader,&SecureFileUploader::download);
    m_udpSocket = new QUdpSocket(this);
    QHostAddress multicastAddress("239.255.20.5"); // 组播地址
    quint16 port = 1365; // 端口号
    qDebug()<<"udp bind statu = "<<m_udpSocket->bind(QHostAddress::AnyIPv4, port, QUdpSocket::ShareAddress);
    qDebug()<<"udp join statu = "<<m_udpSocket->joinMulticastGroup(multicastAddress);

    connect(m_udpSocket, &QUdpSocket::readyRead, this, &TM_SSHConnectWGT::slot_udpReceive);

    progressDialog = new QProgressDialog();
    progressDialog->setWindowTitle(tr("Progress"));
    progressDialog->setLabelText(tr("Please wait..."));
    progressDialog->setRange(0, 100);
    progressDialog->setWindowModality(Qt::WindowModal);
    progressDialog->close();
}

TM_SSHConnectWGT::~TM_SSHConnectWGT()
{
    progressDialog->deleteLater();
}

void TM_SSHConnectWGT::connectStateChanged(bool state, QString ip, int port)
{
    Q_UNUSED(ip);
    Q_UNUSED(port);
    if(state)
    {
        linkStateLabel->setStyleSheet("background-color:transparent;"
                                      "background-image: url(:/Images/normalIcon.png);"
                                      "background-repeat: no-repeat;"
                                      "background-position: center center;");
        cmdLineEdit->setEnabled(true);

        linkBtn->setText("断开");
        linkStateLabel->setStyleSheet("background-image: url(:/Images/normalIcon.png);"
                                      "background-repeat: no-repeat;"
                                      "background-position: center center;");
        hostLineEdit->setDisabled(true);
        portLineEdit->setDisabled(true);
        userNameLineEdit->setDisabled(true);
        passwdLineEdit->setDisabled(true);
        PullBtn->setEnabled(true);
        PushBtn->setEnabled(true);
        Public_ssh_PullFilePath(g_RemoteMapPath+QString("/PLNS.yaml"));

    }
    else
    {
        linkStateLabel->setStyleSheet("background-color:transparent;"
                                      "background-image: url(:/Images/breakIcon.png);"
                                      "background-repeat: no-repeat;"
                                      "background-position: center center;");
        cmdLineEdit->setEnabled(false);

        linkBtn->setText("连接");
        linkStateLabel->setStyleSheet("background-image: url(:/Images/breakIcon.png);"
                                      "background-repeat: no-repeat;"
                                      "background-position: center center;");
        hostLineEdit->setEnabled(true);
        portLineEdit->setEnabled(true);
        userNameLineEdit->setEnabled(true);
        passwdLineEdit->setEnabled(true);
        PullBtn->setEnabled(false);
        PushBtn->setEnabled(false);
    }
}

void TM_SSHConnectWGT::dataArrived(QString msg, QString ip, int port)
{
    Q_UNUSED(ip);
    Q_UNUSED(port);

    static bool is_getMapList = false;
    if(msg.startsWith(QString("ls %1").arg(g_RemoteMapPath)))
    {
        is_getMapList = true;
        m_RemoteMapNameStr = "";
    }
    QStringList dataList = msg.split("\r\n",Qt::SkipEmptyParts);
    for(int i=0;i<dataList.length();i++)
    {
        //qDebug()<<"is_getMapList="<<is_getMapList<<"dataList="<<dataList[i];
        if(is_getMapList)
        {
            if(QString(dataList[i]).indexOf("ls "+g_RemoteMapPath) == -1
                && QString(dataList[i]).indexOf(userNameLineEdit->text()) == -1)
            {
                m_RemoteMapNameStr += QString(dataList[i]) + " ";
            }
            if(QString(dataList[i]).startsWith(userNameLineEdit->text()+"@"+ userNameLineEdit->text()))
            {
                is_getMapList = false;
                m_RemoteMapNameStr = m_RemoteMapNameStr.left(m_RemoteMapNameStr.indexOf("\r\n"));
                m_RemoteMapNameStr = m_RemoteMapNameStr.replace(" ",",");
                qDebug()<<"m_RemoteMapNameStr=="<<m_RemoteMapNameStr;
                emit sig_TcpCmdData("GetMapList",m_RemoteMapNameStr);
                if(m_pushMapName.length() > 0)
                {
                    sshPush(m_pushMapName);
                    m_pushMapName = "";
                }
            }
        }
    }
    textBrowser->append(msg);
}

void TM_SSHConnectWGT::sshConnect()
{
    if(hostLineEdit->text().trimmed().isEmpty() || portLineEdit->text().trimmed().isEmpty() ||
        userNameLineEdit->text().trimmed().isEmpty() || passwdLineEdit->text().trimmed().isEmpty())
    {
        QMessageBox::warning(0,"警告","登录信息未填写完整");
        return;
    }

    if(linkBtn->text() == "连接")
    {
        ssh = new Ssh(hostLineEdit->text(), portLineEdit->text().toUInt(), userNameLineEdit->text(), passwdLineEdit->text());
        connect(ssh, SIGNAL(sigConnectStateChanged(bool,QString,int)), this, SLOT(connectStateChanged(bool,QString,int)));
        connect(ssh, SIGNAL(sigDataArrived(QString,QString,int)), this, SLOT(dataArrived(QString,QString,int)));
        connect(this, SIGNAL(sigSend(QString)), ssh, SLOT(send(QString)));
        connect(this, SIGNAL(sigDisconnected()), ssh, SLOT(sshDisconnected()));
        ssh->init();

        emit sig_TcpCmdData("TcpCon",hostLineEdit->text());
    }
    else
    {
        emit sig_TcpCmdData("TcpDis",hostLineEdit->text());
        emit sigDisconnected();
    }
}

void TM_SSHConnectWGT::executeCmd()
{
    if(cmdLineEdit->text().trimmed().isEmpty()  || ssh == nullptr)
        return;

    if(!ssh->isEnableSSH())
    {
        qDebug() << "SSH 不可用！";
        return;
    }

    QString cmd = cmdLineEdit->text().trimmed();
    // 如果是 ls 命令，去除文字颜色，否则显示一些无关的符号
    if((cmd.left(2) == "ls") || (cmd.left(2) == "ll"))
        cmd += " --color=none";
    cmd += " \r\n";
    qDebug()<<"sshCmd:emit sigSend="<<cmd;
    emit sigSend(cmd);
    cmdLineEdit->clear();
}

void TM_SSHConnectWGT::Pub_executeCmd(QString cmdStr)
{
    if(cmdStr.trimmed().isEmpty() || ssh == nullptr)
        return;
    if(!ssh->isEnableSSH())
    {
        qDebug() << "SSH 不可用！";
        return;
    }

    // 如果是 ls 命令，去除文字颜色，否则显示一些无关的符号
    if((cmdStr.left(2) == "ls") || (cmdStr.left(2) == "ll"))
        cmdStr += " --color=none";
    cmdStr += " \r\n";
    qDebug()<<"sshCmd:emit sigSend="<<cmdStr;
    emit sigSend(cmdStr);
}

void TM_SSHConnectWGT::Public_ssh_Push(QString mapID)
{
    m_pushMapName = mapID;
    Pub_executeCmd(QString("ls %1").arg(g_RemoteMapPath));
}

void TM_SSHConnectWGT::Public_ssh_Pull(QString mapID,QString type)
{
    m_pullType = type;
    sshPull(mapID);
}


void TM_SSHConnectWGT::clearText()
{
    textBrowser->clear();
}

void TM_SSHConnectWGT::sshPush(QString MapID)
{
    qDebug()<<"TM_SSHConnectWGT::ssh__Push-------------";
    m_SShPathList.clear();
    QString strlocalFileDir = g_LocalMapPath+QString("/%1/%2").arg(MapID).arg(MapID);
    QString strRemoteFolder= g_RemoteMapPath+QString("/%1").arg(MapID);

    QString host = hostLineEdit->text();
    QString username = userNameLineEdit->text();
    QString passwd = passwdLineEdit->text();
    int port = portLineEdit->text().toInt();

    static SecureFileUploader uploaderPGM;
    connect(&uploaderPGM,&SecureFileUploader::sig_Transfer,this,&TM_SSHConnectWGT::slot_SSH_Transfer);
    uploaderPGM.upload(strlocalFileDir+".pgm", strRemoteFolder, host, username, passwd, port);
    m_SShPathList.append(strlocalFileDir+".pgm");

    qDebug()<<"m_RemoteMapNameStr============"<<m_RemoteMapNameStr<<MapID;
    if(!m_RemoteMapNameStr.contains(MapID))
    {
        static SecureFileUploader uploaderPBSTREAM;
        connect(&uploaderPBSTREAM,&SecureFileUploader::sig_Transfer,this,&TM_SSHConnectWGT::slot_SSH_Transfer);
        uploaderPBSTREAM.upload(strlocalFileDir+".pbstream", strRemoteFolder, host, username, passwd, port);
        m_SShPathList.append(strlocalFileDir+".pbstream");
    }

    QFile file(strlocalFileDir+".json");
    if(file.exists())
    {
        static SecureFileUploader uploaderJSON;
        connect(&uploaderJSON,&SecureFileUploader::sig_Transfer,this,&TM_SSHConnectWGT::slot_SSH_Transfer);
        uploaderJSON.upload(strlocalFileDir+".json", strRemoteFolder, host, username, passwd, port);
        m_SShPathList.append(strlocalFileDir+".json");
    }

    static SecureFileUploader uploaderYAML;
    connect(&uploaderYAML,&SecureFileUploader::sig_Transfer,this,&TM_SSHConnectWGT::slot_SSH_Transfer);
    uploaderYAML.upload(strlocalFileDir+".yaml", strRemoteFolder, host, username, passwd, port);
    m_SShPathList.append(strlocalFileDir+".yaml");

    textBrowser->append("Start Push---------------"+MapID);

    progressDialog->setLabelText(MapID+tr(" Push Please wait..."));
    progressDialog->setValue(60);
    progressDialog->show();
}

void TM_SSHConnectWGT::sshPull(QString MapID)
{
    m_SShPathList.clear();

    QString strlocalFileDir = g_LocalMapPath+QString("/%1").arg(MapID);
    QString strRemoteFolder= g_RemoteMapPath+QString("/%1/%2").arg(MapID).arg(MapID);

    QDir dir(strlocalFileDir);
    if(!dir.exists())
    {
        //不存在当前目录，创建，可创建多级目录
        dir.mkpath(strlocalFileDir);
    }

    QString host = hostLineEdit->text();
    QString username = userNameLineEdit->text();
    QString passwd = passwdLineEdit->text();
    int port = portLineEdit->text().toInt();

    static SecureFileUploader downloaderPGM;
    connect(&downloaderPGM,&SecureFileUploader::sig_Transfer,this,&TM_SSHConnectWGT::slot_SSH_Transfer);
    downloaderPGM.download(strRemoteFolder+".pgm", strlocalFileDir, host, username, passwd, port);
    m_SShPathList.append(strRemoteFolder+".pgm");

    QFile file(strlocalFileDir+QString("/%1.pbstream").arg(MapID));
    if(!file.exists()) {
        static SecureFileUploader downloaderPBSTREAM;
        connect(&downloaderPBSTREAM,&SecureFileUploader::sig_Transfer,this,&TM_SSHConnectWGT::slot_SSH_Transfer);
        downloaderPBSTREAM.download(strRemoteFolder+".pbstream", strlocalFileDir, host, username, passwd, port);
        m_SShPathList.append(strRemoteFolder+".pbstream");
    }

    static SecureFileUploader downloaderJSON;
    connect(&downloaderJSON,&SecureFileUploader::sig_Transfer,this,&TM_SSHConnectWGT::slot_SSH_Transfer);
    downloaderJSON.download(strRemoteFolder+".json", strlocalFileDir, host, username, passwd, port);
    m_SShPathList.append(strRemoteFolder+".json");

    static SecureFileUploader downloaderYAML;
    connect(&downloaderYAML,&SecureFileUploader::sig_Transfer,this,&TM_SSHConnectWGT::slot_SSH_Transfer);
    downloaderYAML.download(strRemoteFolder+".yaml", strlocalFileDir, host, username, passwd, port);
    m_SShPathList.append(strRemoteFolder+".yaml");

    textBrowser->append("Start Pull---------------"+MapID);
    progressDialog->setLabelText(MapID+tr(" Pull Please wait..."));
    progressDialog->setValue(60);
    progressDialog->show();
}

void TM_SSHConnectWGT::slot_rebootClick()
{
    emit sig_TcpCmdData("TcpDis",hostLineEdit->text());
    cmdLineEdit->setText("sudo reboot -f");
    executeCmd();
    QTimer::singleShot(5000,this,[this](){

        while(tableWidget->rowCount() > 0)
        {
            tableWidget->removeRow(tableWidget->rowCount()-1);
        }
        emit sigDisconnected();
    });

}

void TM_SSHConnectWGT::slot_QSSH_Data(QString Type, QString path)
{
}

void TM_SSHConnectWGT::slot_SSH_Transfer(QString filePath,bool Statu)
{
    qDebug()<<"TM_SSHConnectWGT::slot_SSH_Transfer:"<<filePath<<Statu;
    qDebug()<<"m_SShPathList="<<m_SShPathList.length()<<m_SShPathList;
    if(m_SShPathList.length() <= 0)
        return;
    if(!Statu)
    {
        textBrowser->append("Faile---------------");
        this->setEnabled(true);
        m_SShPathList.clear();
        emit sig_TcpCmdData("pub_pushPull","faile");
    }
    else
    {
        if(filePath.startsWith("No such file:"))
        {
            filePath.replace("No such file:","");
        }
        for(int i = m_SShPathList.length()-1;i>=0;i--)
        {
            if(QString(m_SShPathList[i]) == filePath)
                m_SShPathList.removeAt(i);
        }
        if(m_SShPathList.length() <= 0)
        {
            qDebug()<<"connectPullYAML="<<connectPullYAML;
            textBrowser->append("True---------------");
            this->setEnabled(true);
            progressDialog->setValue(100);

            if(connectPullYAML)
            {
                connectPullYAML = false;
                emit sig_TcpCmdData("pullAmtYAML","true");
            }
            else
            {
                qDebug()<<"sssqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"<<m_pullType;
                if(m_pullType.length() > 0)
                    emit sig_TcpCmdData("pub_pushPull",m_pullType);
                else
                    emit sig_TcpCmdData("pub_pushPull","true");
            }
        }
    }
}

void TM_SSHConnectWGT::slot_TcpConnectStatu(bool statu)
{
    if(!statu)
    {
        QMessageBox messageBox(QMessageBox::Information,QString(u8"信息"),QString(u8"TCP失败,请尝试断开重连"), QMessageBox::Ok, this);
        QTimer timer;
        QObject::connect(&timer, &QTimer::timeout, &messageBox, &QMessageBox::accept);
        timer.setSingleShot(true); // 设置定时器只触发一次
        timer.start(1000); // 5秒后触发定时器
        messageBox.exec(); // 显示消息框
    }
}

void TM_SSHConnectWGT::slot_udpReceive()
{
    while (m_udpSocket->hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(m_udpSocket->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;
        m_udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
        // 处理接收到的数据
        qDebug()<<"slot_udpReceive="<<QString(datagram);
        QStringList messageList = QString(datagram).split("TMRobotIP:");
        while(messageList.length() > 0)
        {
            if(QString(messageList[0]).length() > 7 && !QString(messageList[0]).contains("Z-SEARCH"))
            {
                bool is_have = false;;
                for(int i=0;i<tableWidget->rowCount();i++)
                {
                    if(tableWidget->item(i,0)->text() == QString(messageList[0]))
                    {
                        is_have = true;
                        break;
                    }
                }

                if(!is_have)
                {
                    int row = tableWidget->rowCount();
                    tableWidget->insertRow(row);
                    tableWidget->setItem(row,0,new QTableWidgetItem(QString(messageList[0])));
                }
            }
            messageList.removeAt(0);
        }
    }
}

void TM_SSHConnectWGT::slot_doubleClickItem(QTableWidgetItem *item)
{
    QString text = item->text();
    hostLineEdit->setText(text.mid(0,text.indexOf(",")));
}

void TM_SSHConnectWGT::initUI()
{
    font.setPixelSize(16);
    font.setFamily("黑体");

    hostLabel = new QLabel;
    hostLabel->setFont(font);
    hostLabel->setText("主机IP：");
    hostLabel->setFixedSize(90,32);
    hostLabel->setAlignment(Qt::AlignVCenter|Qt::AlignRight);

    hostLineEdit = new QLineEdit;
    font.setFamily("Times New Roman");
    hostLineEdit->setFixedSize(200,32);
    hostLineEdit->setFont(font);
    hostLineEdit->setAlignment(Qt::AlignCenter);
    hostLineEdit->setPlaceholderText("IP");
    hostLineEdit->setToolTip("请输入IP");
    QRegExp ipExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
    QRegExpValidator * ipVal = new QRegExpValidator(ipExp);
    hostLineEdit->setValidator(ipVal);
    hostLineEdit->setStyleSheet("background-color:rgba(255,255,255,0.1); border-radius: 3px; margin:0px;");
    hostLineEdit->setText("192.168.128.39");

    font.setFamily("黑体");
    portLabel = new QLabel;
    portLabel->setFont(font);
    portLabel->setText("端口：");
    portLabel->setFixedSize(90,32);
    portLabel->setAlignment(Qt::AlignVCenter|Qt::AlignRight);

    portLineEdit = new QLineEdit;
    font.setFamily("Times New Roman");
    portLineEdit->setFixedSize(200,32);
    portLineEdit->setFont(font);
    portLineEdit->setAlignment(Qt::AlignCenter);
    portLineEdit->setPlaceholderText("端口号");
    portLineEdit->setToolTip("请输入端口号");
    QIntValidator * portVal = new QIntValidator(0,65535);
    portLineEdit->setValidator(portVal);
    portLineEdit->setStyleSheet("background-color:rgba(255,255,255,0.1); border-radius: 3px; margin:0px;");
    portLineEdit->setText("22");

    font.setFamily("黑体");
    userNameLabel = new QLabel;
    userNameLabel->setFont(font);
    userNameLabel->setText("用户名：");
    userNameLabel->setFixedSize(90,32);
    userNameLabel->setAlignment(Qt::AlignVCenter|Qt::AlignRight);
    userNameLabel->setStyleSheet("margin:0px;");

    userNameLineEdit = new QLineEdit;
    font.setFamily("Times New Roman");
    userNameLineEdit->setFixedSize(200,32);
    userNameLineEdit->setFont(font);
    userNameLineEdit->setAlignment(Qt::AlignCenter);
    userNameLineEdit->setPlaceholderText("用户名");
    userNameLineEdit->setToolTip("请输入用户名");
    QRegExp userNameExp("^[a-zA-Z0-9_][a-zA-Z0-9_]+$");
    QRegExpValidator * userNameVal = new QRegExpValidator(userNameExp);
    userNameLineEdit->setValidator(userNameVal);
    userNameLineEdit->setStyleSheet("background-color:rgba(255,255,255,0.1); border-radius: 3px; margin:0px;");
    userNameLineEdit->setText("tami");

    font.setFamily("黑体");
    passwdLabel = new QLabel;
    passwdLabel->setFont(font);
    passwdLabel->setText("密码：");
    passwdLabel->setFixedSize(90,32);
    passwdLabel->setAlignment(Qt::AlignVCenter|Qt::AlignRight);

    passwdLineEdit = new QLineEdit;
    font.setFamily("Times New Roman");
    passwdLineEdit->setFixedSize(200,32);
    passwdLineEdit->setFont(font);
    passwdLineEdit->setAlignment(Qt::AlignCenter);
    passwdLineEdit->setPlaceholderText("密码");
    passwdLineEdit->setEchoMode(QLineEdit::Password);
    passwdLineEdit->setToolTip("请输入密码");
    passwdLineEdit->setStyleSheet("background-color:rgba(255,255,255,0.1); border-radius: 3px;margin:0px;");
    passwdLineEdit->setText("x");

    linkStateLabel = new QLabel;
    linkStateLabel->setFixedSize(90,32);
    linkStateLabel->setStyleSheet("background-image: url(:/Images/breakIcon.png);"
                                  "background-repeat: no-repeat;"
                                  "background-position: center center;");

    linkBtn = new QPushButton;
    font.setFamily("黑体");
    linkBtn->setFont(font);
    linkBtn->setText("连接");
    linkBtn->setStyleSheet("QPushButton{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #5EDCF8, stop:0.5 #82E5FB, stop:1 #06C9F4); color:rgb(255,255,255); border-radius:8px; margin:0px;}"
                           "QPushButton:pressed{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #c0c0c0, stop:1 #808080); color:rgb(255,255,255); border-radius:8px; margin:0px;}"
                           "QPushButton:disabled{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #c0c0c0, stop:1 #808080); color:rgb(255,255,255); border-radius:8px; margin:0px;}");
    linkBtn->setFixedSize(112,32);
    connect(linkBtn, SIGNAL(clicked()), this, SLOT(sshConnect()));

    PushBtn  = new QPushButton;
    font.setFamily("黑体");
    PushBtn->setFont(font);
    PushBtn->setText("Push");
    PushBtn->setStyleSheet("QPushButton{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #5EDCF8, stop:0.5 #82E5FB, stop:1 #06C9F4); color:rgb(255,255,255); border-radius:8px; margin:0px;}"
                           "QPushButton:pressed{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #c0c0c0, stop:1 #808080); color:rgb(255,255,255); border-radius:8px; margin:0px;}"
                           "QPushButton:disabled{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #c0c0c0, stop:1 #808080); color:rgb(255,255,255); border-radius:8px; margin:0px;}");
    PushBtn->setFixedSize(112,32);
    PushBtn->setEnabled(false);

    PullBtn = new QPushButton;
    font.setFamily("黑体");
    PullBtn->setFont(font);
    PullBtn->setText("重启底盘");
    PullBtn->setStyleSheet("QPushButton{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #5EDCF8, stop:0.5 #82E5FB, stop:1 #06C9F4); color:rgb(255,255,255); border-radius:8px; margin:0px;}"
                           "QPushButton:pressed{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #c0c0c0, stop:1 #808080); color:rgb(255,255,255); border-radius:8px; margin:0px;}"
                           "QPushButton:disabled{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #c0c0c0, stop:1 #808080); color:rgb(255,255,255); border-radius:8px; margin:0px;}");
    PullBtn->setFixedSize(112,32);
    PullBtn->setEnabled(false);
    connect(PullBtn,&QPushButton::clicked,this,&TM_SSHConnectWGT::slot_rebootClick);

    textBrowser = new QTextBrowser;
    font.setFamily("Times New Roman");
    textBrowser->setFont(font);
    textBrowser->setStyleSheet("background-color:rgba(255,255,255,0.1);border-radius: 3px;border-color: rgba(255,255,255);margin:0px;");

    tableWidget = new QTableWidget;
    connect(tableWidget,&QTableWidget::itemDoubleClicked,this,&TM_SSHConnectWGT::slot_doubleClickItem);
    tableWidget->setColumnCount(1);
    QStringList headers;
    headers << "IP" ;
    tableWidget->setHorizontalHeaderLabels(headers);
    //tableWidget->setColumnWidth(0,50);
    tableWidget->horizontalHeader()->setSectionResizeMode(0,QHeaderView::Stretch);
    tableWidget->verticalHeader()->setStyleSheet("QHeaderView::section { background-color: white; color: black; }");
    tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section { background-color: white; color: black; }");

    tableWidget->setStyleSheet("background-color:rgba(255,255,255,0.1); border-radius: 3px; margin:0px;");
    tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    cmdLabel = new QLabel;
    font.setFamily("黑体");
    cmdLabel->setFont(font);
    cmdLabel->setText("命令(ENTER执行)：");
    cmdLabel->setFixedSize(140,32);
    cmdLabel->setAlignment(Qt::AlignVCenter|Qt::AlignRight);

    cmdLineEdit = new QLineEdit;
    font.setFamily("Times New Roman");
    cmdLineEdit->setFixedHeight(32);
    cmdLineEdit->setFont(font);
    cmdLineEdit->setAlignment(Qt::AlignCenter);
    cmdLineEdit->setPlaceholderText("指令");
    cmdLineEdit->setToolTip("请输入指令");
    cmdLineEdit->setStyleSheet("background-color:rgba(255,255,255,0.1); border-radius: 3px; margin:0px;");
    cmdLineEdit->setText(QString("cd %1").arg(g_RemoteMapPath));
    cmdLineEdit->setEnabled(false);
    connect(cmdLineEdit, SIGNAL(returnPressed()), this, SLOT(executeCmd()));

    clearBtn = new QPushButton;
    font.setFamily("黑体");
    clearBtn->setFont(font);
    clearBtn->setText("清空");
    clearBtn->setStyleSheet("QPushButton{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #5EDCF8, stop:0.5 #82E5FB, stop:1 #06C9F4); color:rgb(255,255,255); border-radius:8px; margin:0px;}"
                            "QPushButton:pressed{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #c0c0c0, stop:1 #808080); color:rgb(255,255,255); border-radius:8px; margin:0px;}"
                            "QPushButton:disabled{background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #c0c0c0, stop:1 #808080); color:rgb(255,255,255); border-radius:8px; margin:0px;}");
    clearBtn->setFixedSize(60,32);
    connect(clearBtn, SIGNAL(clicked()), this, SLOT(clearText()));

    QHBoxLayout * hostLayout = new QHBoxLayout;
    hostLayout->addWidget(hostLabel);
    hostLayout->addWidget(hostLineEdit);

    QHBoxLayout * portLayout = new QHBoxLayout;
    portLayout->addWidget(portLabel);
    portLayout->addWidget(portLineEdit);

    QHBoxLayout * userNameLayout = new QHBoxLayout;
    userNameLayout->addWidget(userNameLabel);
    userNameLayout->addWidget(userNameLineEdit);

    QHBoxLayout * passwdLayout = new QHBoxLayout;
    passwdLayout->addWidget(passwdLabel);
    passwdLayout->addWidget(passwdLineEdit);

    QHBoxLayout * linkLayout = new QHBoxLayout;
    //linkLayout->addWidget(linkStateLabel);
    linkLayout->addWidget(PushBtn,Qt::AlignVCenter|Qt::AlignHCenter);
    linkLayout->addWidget(PullBtn,Qt::AlignVCenter|Qt::AlignHCenter);
    linkLayout->addWidget(linkBtn,Qt::AlignVCenter|Qt::AlignHCenter);


    QHBoxLayout * cmdLayout = new QHBoxLayout;
    cmdLayout->addWidget(cmdLabel);
    cmdLayout->addWidget(cmdLineEdit);
    cmdLayout->addWidget(clearBtn);

    QVBoxLayout * leftLayout = new QVBoxLayout;
    leftLayout->addLayout(hostLayout);
    leftLayout->addLayout(portLayout);
    leftLayout->addLayout(userNameLayout);
    leftLayout->addLayout(passwdLayout);
    leftLayout->addLayout(linkLayout);

    QVBoxLayout * rightLayout = new QVBoxLayout;
    rightLayout->addWidget(tableWidget);
    rightLayout->addWidget(textBrowser);


    QHBoxLayout * topLayout = new QHBoxLayout;
    topLayout->addLayout(leftLayout);
    topLayout->addLayout(rightLayout);

    QVBoxLayout * mainLayout = new QVBoxLayout;
    mainLayout->addLayout(topLayout,0);
    mainLayout->addLayout(cmdLayout);
    setLayout(mainLayout);
}

void TM_SSHConnectWGT::Public_ssh_PullFilePath(QString path)
{
    m_SShPathList.clear();
    QString strlocalFileDir = g_LocalMapPath;
    QString strRemoteFolder = path;
    QDir dir(strlocalFileDir);
    if(!dir.exists())
    {
        //不存在当前目录，创建，可创建多级目录
        dir.mkpath(strlocalFileDir);
    }

    QString host = hostLineEdit->text();
    QString username = userNameLineEdit->text();
    QString passwd = passwdLineEdit->text();
    int port = portLineEdit->text().toInt();

    static SecureFileUploader downloaderFile;
    connect(&downloaderFile,&SecureFileUploader::sig_Transfer,this,&TM_SSHConnectWGT::slot_SSH_Transfer);
    downloaderFile.download(strRemoteFolder, strlocalFileDir, host, username, passwd, port);
    m_SShPathList.append(strRemoteFolder);
    textBrowser->append("Start Pull---------------");
    progressDialog->setLabelText(tr("Pull Please wait..."));
    progressDialog->setValue(60);
    progressDialog->show();

    connectPullYAML = true;
}

void TM_SSHConnectWGT::Public_ssh_PushFilePath(QString path)
{
    m_SShPathList.clear();
    QString strlocalFileDir = g_LocalMapPath+QString("/%1").arg(path);
    QString strRemoteFolder= g_RemoteMapPath;

    QString host = hostLineEdit->text();
    QString username = userNameLineEdit->text();
    QString passwd = passwdLineEdit->text();
    int port = portLineEdit->text().toInt();

    static SecureFileUploader uploaderFile;
    connect(&uploaderFile,&SecureFileUploader::sig_Transfer,this,&TM_SSHConnectWGT::slot_SSH_Transfer);
    uploaderFile.upload(strlocalFileDir, strRemoteFolder, host, username, passwd, port);
    m_SShPathList.append(strlocalFileDir);

    textBrowser->append("Start Push---------------"+path);
    progressDialog->setLabelText(tr("Push Please wait..."));
    progressDialog->setValue(60);
    progressDialog->show();
}
